﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using Taurus.BLL;
using Taurus.DTO;

namespace Taurus
{
    public partial class PlugsUserControl : UserControl
    {
      
        public PlugsUserControl()
        {
            InitializeComponent();
        }

        private void InitData()
        {
            unloadComponentListBox.Items.Clear();
            loadComponentListBox.Items.Clear();
            //启用
            foreach (var item in ComponentsBll.AllComponents)
            {
                KryptonListItem li = new KryptonListItem();
                li.ShortText = item.GetComponent().Name;
                li.LongText = item.GetComponent().Version;
                li.Tag = item.GetComponent();
                li.Image = global::Taurus.Properties.Resources.plugin;
                loadComponentListBox.Items.Add(li);
            }
            //未启用
            foreach (var item in ComponentsBll.AllUnloadComponents)
            {
                KryptonListItem li = new KryptonListItem();
                li.ShortText = item.GetComponent().Name;
                li.LongText = item.GetComponent().Version;
                li.Tag = item.GetComponent();
                li.Image = global::Taurus.Properties.Resources.plugin_disabled;
                unloadComponentListBox.Items.Add(li);
            }
            
        }

        #region event

        private void PlugsUserControl_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void kryptonButton3_Click(object sender, EventArgs e)
        {
            //TODO:刷新
            MessageBox.Show("该功能未完成");
        }

        //加载
        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            if (unloadComponentListBox.SelectedItem != null)
            {
                KryptonListItem li = (KryptonListItem)unloadComponentListBox.SelectedItem;
                unloadComponentListBox.Items.RemoveAt(unloadComponentListBox.SelectedIndex);
                li.Image = global::Taurus.Properties.Resources.plugin;
                loadComponentListBox.Items.Add(li);

                global::Taurus.Components.Component com = (global::Taurus.Components.Component)li.Tag;
                ComponentDTO config = new ComponentDTO();
                config.UniqueID = com.Unique;
                config.Version = com.Version;
                config.Name = com.Name;
                ComponentsBll.UpdateComponentConfig(config);

            }

        }

        //卸载
        private void kryptonButton2_Click(object sender, EventArgs e)
        {
            if (loadComponentListBox.SelectedItem != null)
            {
                KryptonListItem li = (KryptonListItem)loadComponentListBox.SelectedItem;
                loadComponentListBox.Items.RemoveAt(loadComponentListBox.SelectedIndex);
                li.Image = global::Taurus.Properties.Resources.plugin_disabled;
                unloadComponentListBox.Items.Add(li);

                global::Taurus.Components.Component com = (global::Taurus.Components.Component)li.Tag;
                ComponentDTO config = new ComponentDTO();
                config.UniqueID = com.Unique;
                config.Version = com.Version;
                config.Name = com.Name;
                ComponentsBll.DeleteComponentConfig(config);
            }
        }

        #endregion

        
    }
}
